package com.api.gbhg.daiyanlu.Dao;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetTrans;

import java.util.Map;

public class RecordSubjectsDao {
    private static final Logger log = LoggerFactory.getLogger("gbhg");

    /**
     * 删除
     * @param tableGxmdygnkm
     * @param requestId
     * @return
     */
    public static void deleteOne(String tableGxmdygnkm, String requestId) {
        RecordSet rs = new RecordSet();
        String sql = "delete from " + tableGxmdygnkm + " where xmlcid = " + requestId + "";
        rs.execute(sql);
    }

    /**
     * 根据workflow_currentoperator表的requestId查找报销流程id
     *
     * @param requestId
     * @return
     */
    public static String getRequestIdDao(String requestId) {
        String sql = "select * from workflow_currentoperator where requestid in (" + requestId + ") ";
        RecordSet rs = new RecordSet();
        rs.executeQuery(sql);
        rs.next();
        return rs.getString("workflowid");
    }

    /**
     * 根据workflow_bill表的id查找报销流程表名
     *
     * @param formId
     * @return
     */
    public static String getFromId(String formId) {
        String sql = "select * from workflow_bill where id =" + formId + "";
        RecordSet rs = new RecordSet();
        rs.executeQuery(sql);
        rs.next();
        return rs.getString("tablename");
    }

    /**
     * 查询流程Id
     *
     * @param requestId
     * @param tbName
     * @return
     */
    public static String tableId(String requestId, String tbName) {
        String sql = "select * from " + tbName + " where requestId=" + requestId + "";
        RecordSet rs = new RecordSet();
        rs.executeQuery(sql);
        rs.next();
        return rs.getString("id");
    }

    /**
     * 查询流程表上年结转
     *
     * @param id
     * @param tbName
     * @return
     */
    public static RecordSet lastYear(String id, String tbName) {
        RecordSet rs = new RecordSet();
        String sql = "select (d3.zyczbksr)lastYear,(d3.srlx)dt3,f.ysnd,f.sbdw,(d3.dfczbkxmsnjz)z3,zcqd from " + tbName + " f join " + tbName + "_dt3 d3\n" +
                "on f.id = d3.mainid  where d3.mainid = " + id + "";
        rs.executeQuery(sql);
        return rs;
    }

    /**
     * 查询流程表当年结转
     *
     * @param id
     * @param tbName
     * @return
     */
    public static RecordSet thoseYear(String id, String tbName) {
        RecordSet rs = new RecordSet();
        String sql = "select (d4.zyczbksr)thoseYear,(d4.srlx)dt4,ysnd,sbdw,(d4.dfczbkxmdnys)z4,zcqd from " + tbName + " f join " + tbName + "_dt4 d4\n" +
                "on f.id = d4.mainid  where d4.mainid = " + id + "";
        rs.executeQuery(sql);
        return rs;
    }

    /**
     * 查询流程表上年结转02流程
     *
     * @param id
     * @param tbName
     * @return
     */
    public static RecordSet lastYear2(String id, String tbName) {
        RecordSet rs = new RecordSet();
        String sql = "select (d3.zyczbksr)lastYear,(d3.srlx)dt3,f.ysnd,f.sbdw,(d3.dfczbkxmsnjz)z3,zcqd from " + tbName + " f join " + tbName + "_dt5 d3 \n" +
                "on f.id = d3.mainid  where d3.mainid = " + id + "";
        rs.executeQuery(sql);
        log.info("02特殊流程上年结转sql：" + sql);
        return rs;
    }

    /**
     * 查询流程表当年结转02
     *
     * @param id
     * @param tbName
     * @return
     */
    public static RecordSet thoseYear2(String id, String tbName) {
        RecordSet rs = new RecordSet();
        String sql = "select (d4.zyczbksr)thoseYear,(d4.srlx)dt4,ysnd,sbdw,(d4.dfczbkxmdnys)z4,zcqd from " + tbName + " f join " + tbName + "_dt6 d4 \n" +
                "on f.id = d4.mainid  where d4.mainid = " + id + "";
        rs.executeQuery(sql);
        log.info("02特殊流程当年结转sql：" + sql);
        return rs;
    }

    /**
     * 查询流程表上年结转18流程
     *
     * @param id
     * @param tbName
     * @return
     */
    public static RecordSet lastYear18(String id, String tbName) {
        RecordSet rs = new RecordSet();
        String sql = "select (d3.zyczbksr)lastYear,(d3.srlx)dt3,f.ysnd,f.sbdw,(d3.dfczbkxmsnjz)z3,zcqd from " + tbName + " f join " + tbName + "_dt7 d3 \n" +
                "on f.id = d3.mainid  where d3.mainid = " + id + "";
        rs.executeQuery(sql);
        return rs;
    }

    /**
     * 查询流程表当年结转18
     *
     * @param id
     * @param tbName
     * @return
     */
    public static RecordSet thoseYear18(String id, String tbName) {
        RecordSet rs = new RecordSet();
        String sql = "select (d4.zyczbksr)thoseYear,(d4.srlx)dt4,ysnd,sbdw,(d4.dfczbkxmdnys)z4,zcqd from " + tbName + " f join " + tbName + "_dt8 d4 \n" +
                "on f.id = d4.mainid  where d4.mainid = " + id + "";
        rs.executeQuery(sql);
        return rs;
    }

    /**
     * 查询流程表上年结转表05
     *
     * @param id
     * @param tbName
     * @return
     */
    public static RecordSet lastYear5(String id, String tbName) {
        RecordSet rs = new RecordSet();
        String sql = "select (d3.zyczbksr)lastYear,(d3.srlx)dt3,ysnd,sbdw,(d3.dfczbkxmsnjz)z3,zcqd from " + tbName + " f join " + tbName + "_dt4 d3 \n" +
                "on f.id = d3.mainid  where d3.mainid = " + id + "";
        rs.executeQuery(sql);
        return rs;
    }

    /**
     * 查询流程表当年结转05
     *
     * @param id
     * @param tbName
     * @return
     */
    public static RecordSet thoseYear5(String id, String tbName) {
        RecordSet rs = new RecordSet();
        String sql = "select (d4.zyczbksr)thoseYear,(d4.srlx)dt4,ysnd,sbdw,(d4.dfczbkxmdnys)z4,zcqd from " + tbName + " f join " + tbName + "_dt5 d4 \n" +
                "on f.id = d4.mainid  where d4.mainid = " + id + "";
        rs.executeQuery(sql);
        return rs;
    }

    /**
     * 非中央收入存储类别的数据表
     *
     * @param zcqd
     * @return
     */
    public static String sfwdlhsdw(int zcqd, int year, int sbdw) {
        String sql = "select * from uf_fzysr where szfl=" + zcqd + " and nf=" + year + " and dw=" + sbdw + "";
        RecordSet rs = new RecordSet();
        rs.executeQuery(sql);
        rs.next();
        return rs.getString("sfwdlhsdw");
    }

    /**
     * 非中央收入存储类别的数据表
     *
     * @param zcqd
     * @return
     */
//    public static String theCentral1(int zcqd, int year, int sbdw) {
//        String sql = "select * from uf_fzysr where szfl=" + zcqd + " and nf=" + year + " and dw=" + sbdw + "";
//        RecordSet rs = new RecordSet();
//        rs.executeQuery(sql);
//        rs.next();
//        return rs.getString("gnkm");
//    }

    /**
     * 非中央收入存储类别的数据表
     *
     * @param zcqd
     * @return
     */
    public static String theCentral1(int zcqd,int year,int sbdw){
        String sql = "select * from uf_fzysr where szfl="+zcqd+" and nf="+year+" and dw="+sbdw+"";
        RecordSet rs = new RecordSet();
        rs.executeQuery(sql);
        rs.next();
        return rs.getString("gnkm");
    }

    /**
     * 非中央收入存储类别的数据表
     *
     * @param zcqd
     * @return
     */
    public static String theCentral2(int zcqd,int year,int sbdw){
        String sql = "select * from uf_fzysr where szfl="+zcqd+" and nf="+year+" and dw="+sbdw+"";
        RecordSet rs = new RecordSet();
        rs.executeQuery(sql);
        rs.next();
        return rs.getString("szfl");
    }


    /**
     * 功能科目表(非中央查询)上年
     *
     * @param gnkm
     * @return
     */
    public static RecordSet theCentral2(String gnkm,int year) {
        RecordSet rs = new RecordSet();
        String sql = "select * from uf_gnkm where id=" + gnkm + " and nd="+year+"";
        rs.executeQuery(sql);
        return rs;
    }

    /**
     *收入预算分类表
     * @param szfl
     * @return
     */
    public static String classify(String szfl){
        String sql = "select * from uf_srysfl where id = "+szfl+"";
        RecordSet rs = new RecordSet();
        rs.executeQuery(sql);
        rs.next();
        return rs.getString("id");
    }

    /**
     * 非中央项目表
     *
     * @param id
     * @return
     */
    public static String table1(String id) {
        String sql = "select * from uf_fzysrxm where id=" + id + "";
        RecordSet rs = new RecordSet();
        rs.executeQuery(sql);
        rs.next();
        return rs.getString("szfl");
    }

    /**
     * 非中央类别表
     * @param id
     * @return
     */
    public static String table2(String id){
        RecordSet rs = new RecordSet();
        String sql = "select * from uf_fzysr where id="+id+"";
        rs.executeQuery(sql);
        rs.next();
        return rs.getString("gnkm");
    }

    /**
     * 非中央类别表
     * @param id
     * @return
     */
    public static String table3(String id){
        RecordSet rs = new RecordSet();
        String sql = "select * from uf_fzysr where id="+id+"";
        rs.executeQuery(sql);
        rs.next();
        return rs.getString("szfl");
    }


    /**
     * 功能科目表(非中央查询)当年
     * @param gnkm
     * @return
     */
    public static RecordSet theCentral3(String gnkm,int year){
        RecordSet rs = new RecordSet();
        String sql = "select * from uf_gnkm where id="+gnkm+" and nd="+year+"";
        rs.executeQuery(sql);
        return rs;
    }

    /**
     * 功能科目表(中央查询)上年
     *
     * @param kmdm
     * @return
     */
    public static RecordSet functionSubjects(int kmdm) {
        RecordSet rs = new RecordSet();
        String sql = "select * from uf_gnkm where id = " + kmdm + " ";
        rs.executeQuery(sql);
        log.info("查询功能科目表" + sql);
        return rs;
    }

    /**
     * 功能科目表(中央查询)当年年
     *
     * @param kmdm
     * @return
     */
    public static RecordSet functionSubjects2(int kmdm) {
        RecordSet rs = new RecordSet();
        String sql = "select * from uf_gnkm where id = " + kmdm + " ";
        rs.executeQuery(sql);
        log.info("功能科目表查询：" + sql);
        return rs;
    }

    /**
     * 科目表
     *
     * @param sum2
     * @return
     */
    public static int subjects(int sum2) {
        String sql = "select * from uf_zyczbksr_km where id = " + sum2 + "";
        RecordSet rs = new RecordSet();
        rs.executeQuery(sql);
        rs.next();
        return rs.getInt("gnkm");
    }

    /**
     * 科目下级表
     *
     * @param sum2
     * @return
     */
    public static int subjectsSubordinate(int sum2) {
        String sql = "select * from uf_zyczbksr_xj where id = " + sum2 + " ";
        RecordSet rs = new RecordSet();
        rs.executeQuery(sql);
        rs.next();
        return rs.getInt("szgnkm");
    }

    public static String subjectsSubordinate1(int sum2) {
        String sql = "select * from uf_zyczbksr_xj where id = " + sum2 + " ";
        RecordSet rs = new RecordSet();
        rs.executeQuery(sql);
        rs.next();
        return rs.getString("srzx");
    }

    /**
     * 获取建模表id
     *
     * @param tableName
     * @return
     */
    public static String getModeId(String tableName) {
        String sql = "select id from modeinfo where formid=(select id from workflow_bill where tablename='" + tableName + "')";
        RecordSet rs = new RecordSet();
        rs.executeQuery(sql);
        rs.next();
        return rs.getString("id");
    }

    /**
     * 往uf_gxmdygnkm表中添加数据
     *
     * @param param
     * @return
     */
    public boolean insertBudgeLines(Map<String, String> param) {
        RecordSetTrans rts = new RecordSetTrans();
        String sql = "insert into uf_gxmdygnkm(xmlcid,dygnkmmc,kmdm,dygnkmbid,zfqd,lb,modedatacreater,modedatacreatedate,modedatacreatertype,modedatacreatetime,formmodeid,modeuuid,kmjxj,hpssjid)values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        try {
            rts.executeUpdate(sql,
                    param.getOrDefault("xmlcid", ""),
                    param.getOrDefault("dygnkmmc", ""),
                    param.getOrDefault("kmdm", ""),
                    param.getOrDefault("dygnkmbid", ""),
                    param.getOrDefault("zfqd", ""),
                    param.getOrDefault("lb", ""),
                    param.getOrDefault("modedatacreater", ""),
                    param.getOrDefault("modedatacreatedate", ""),
                    param.getOrDefault("modedatacreatertype", ""),
                    param.getOrDefault("modedatacreatetime", ""),
                    param.getOrDefault("formmodeid", ""),
                    param.getOrDefault("modeuuid", ""),
                    param.getOrDefault("kmjxj", ""),
                    param.getOrDefault("hpssjid", "")
            );
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            rts.rollback();
        }
        log.info("插入成功" + sql);
        return false;
    }

    /**
     * 往uf_glchpfzcbk表中添加数据
     *
     * @param param
     * @return
     */
    public boolean insertBudgeLines1(Map<String, String> param) {
        RecordSetTrans rts = new RecordSetTrans();
        String sql = "insert into  uf_glchpfzcbk (xmlcid,yssrfl,fczbkxm,jb,zfqd,hpssjid,modedatacreater,modedatacreatedate,modedatacreatertype,modedatacreatetime,formmodeid,modeuuid,nd)values(?,?,?,?,?,?,?,?,?,?,?,?,?)";
        try {
            rts.executeUpdate(sql,
                    param.getOrDefault("xmlcid", ""),
                    param.getOrDefault("yssrfl", ""),
                    param.getOrDefault("fczbkxm", ""),
                    param.getOrDefault("jb", ""),
                    param.getOrDefault("zfqd", ""),
                    param.getOrDefault("hpssjid", ""),
                    param.getOrDefault("modedatacreater", ""),
                    param.getOrDefault("modedatacreatedate", ""),
                    param.getOrDefault("modedatacreatertype", ""),
                    param.getOrDefault("modedatacreatetime", ""),
                    param.getOrDefault("formmodeid", ""),
                    param.getOrDefault("modeuuid", ""),
                    param.getOrDefault("nd", "")
            );
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            rts.rollback();
        }
        log.info("插入成功" + sql);
        return false;
    }

    /**
     * 根据workflow_base表的formId查找报销流程表名id
     *
     * @param id
     * @return
     */
    public static String getIdDao(String id) {
        String sql = "select * from workflow_base where id =" + id + " ";
        RecordSet rs = new RecordSet();
        rs.executeQuery(sql);
        rs.next();
        return rs.getString("formid");
    }

}
